﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace MLRobotic.Tower.Detection
{
    public static class ColorHelper
    {
        public static float GetHue(Color color)
        {
            return GetHue(color.R, color.G, color.B);
        }

        public static float GetSaturation(Color color)
        {
            return GetSaturation(color.R, color.G, color.B);
        }

        public static float GetBrightness(Color color)
        {
            return GetBrightness(color.R, color.G, color.B);
        }

        public static float GetHue(byte r, byte g, byte b)
        {
            if (r == g && g == b)
                return 0.0f;

            float R = (float)r / (float)byte.MaxValue;
            float G = (float)g / (float)byte.MaxValue;
            float B = (float)b / (float)byte.MaxValue;

            float hue = 0.0f;
            float max = R;
            float min = R;

            if ((double)G > (double)max)
                max = G;
            if ((double)B > (double)max)
                max = B;
            if ((double)G < (double)min)
                min = G;
            if ((double)B < (double)min)
                min = B;

            float chroma = max - min;

            if ((double)R == (double)max)
                hue = (G - B) / chroma;
            else if ((double)G == (double)max)
                hue = (float)(2.0 + ((double)B - (double)R) / (double)chroma);
            else if ((double)B == (double)max)
                hue = (float)(4.0 + ((double)R - (double)G) / (double)chroma);


            hue = hue * 60f;

            if ((double)hue < 0.0)
                hue += 360f;

            return hue;
        }

        public static float GetSaturation(byte r, byte g, byte b)
        {
            float R = (float)r / (float)byte.MaxValue;
            float G = (float)g / (float)byte.MaxValue;
            float B = (float)b / (float)byte.MaxValue;
            float saturation = 0.0f;
            float max = R;
            float min = R;

            if ((double)G > (double)max)
                max = G;
            if ((double)B > (double)max)
                max = B;
            if ((double)G < (double)min)
                min = G;
            if ((double)B < (double)min)
                min = B;

            if ((double)max != (double)min)
                saturation = ((double)max + (double)min) / 2.0 > 0.5 ?
                      (float)(((double)max - (double)min) / (2.0 - (double)max - (double)min)) 
                    : (float)(((double)max - (double)min) / ((double)max + (double)min));
            
            return saturation;
        }

        public static float GetBrightness(byte r, byte g, byte b)
        {
            float R = (float)r / (float)byte.MaxValue;
            float G = (float)g / (float)byte.MaxValue;
            float B = (float)b / (float)byte.MaxValue;
            float max = R;
            float min = R;

            if ((double)G > (double)max)
                max = G;
            if ((double)B > (double)max)
                max = B;
            if ((double)G < (double)min)
                min = G;
            if ((double)B < (double)min)
                min = B;

            return (float)((max + min) / 2.0);
        }

    }
}
